﻿@*
    登录页面 - 独立的视图页面
*@
@{
    Layout = null; // 不使用布局，独立页面
}

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MES制造执行系统 - 登录</title>
    <link href="~/layui/css/layui.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            padding: 40px;
            width: 400px;
            text-align: center;
        }

        .login-header {
            margin-bottom: 30px;
        }

        .system-logo {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #1e88e5, #1565c0);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            color: white;
            font-size: 32px;
        }

        .system-title {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin-bottom: 10px;
        }

        .system-subtitle {
            color: #666;
            font-size: 14px;
        }

        .login-form {
            text-align: left;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }

        .form-input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .form-input:focus {
            outline: none;
            border-color: #1e88e5;
            box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
        }

        .form-input::placeholder {
            color: #999;
        }

        .button-group {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }

        .btn {
            flex: 1;
            padding: 12px 20px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: linear-gradient(135deg, #1e88e5, #1565c0);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(30, 136, 229, 0.3);
        }

        .btn-secondary {
            background: white;
            color: #1e88e5;
            border: 2px solid #1e88e5;
        }

        .btn-secondary:hover {
            background: #1e88e5;
            color: white;
            transform: translateY(-2px);
        }

        .error-message {
            color: #f44336;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <div class="system-logo">
                <i class="fas fa-industry"></i>
            </div>
            <h1 class="system-title">MES制造执行系统</h1>
            <p class="system-subtitle">Manufacturing Execution System</p>
        </div>

        <form class="login-form" id="loginForm">
            <div class="form-group">
                <label class="form-label">用户名</label>
                <input type="text" id="names" class="form-input" placeholder="请输入用户名" required>
                <div class="error-message" id="usernameError">用户名不能为空</div>
            </div>

            <div class="form-group">
                <label class="form-label">密码</label>
                <input type="password" id="pwds" class="form-input" placeholder="请输入密码" required>
                <div class="error-message" id="passwordError">密码不能为空</div>
            </div>

            <div class="button-group">
                <button type="button" class="btn btn-primary" onclick="login()">
                    <i class="fas fa-sign-in-alt"></i> 登录
                </button>
                <button type="button" class="btn btn-secondary" onclick="rejus()">
                    <i class="fas fa-user-plus"></i> 注册
                </button>
            </div>
        </form>
    </div>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/layui/layui.js"></script>
    <script src="~/js.cookie.js"></script>

    <script>
        $(function () {
            // 回车键登录
            $('#pwds').keypress(function(e) {
                if(e.which == 13) {
                    login();
                }
            });

            // 输入时隐藏错误信息
            $('#names, #pwds').on('input', function() {
                $(this).siblings('.error-message').hide();
            });
        });

        function login() {
            var names = $("#names").val().trim();
            var pwds = $("#pwds").val().trim();
            
            // 重置错误信息
            $('.error-message').hide();
            
            // 验证用户名
            if (names === "") {
                $("#usernameError").show();
                $("#names").focus();
                return;
            }
            
            // 验证密码
            if (pwds === "") {
                $("#passwordError").show();
                $("#pwds").focus();
                return;
            }

            // 显示加载状态
            var $btn = $('.btn-primary');
            var originalText = $btn.html();
            $btn.html('<i class="fas fa-spinner fa-spin"></i> 登录中...').prop('disabled', true);
            
            $.ajax({
                url: 'http://localhost:5088/api/Manage/Login?UserName=' + encodeURIComponent(names) + '&Password=' + encodeURIComponent(pwds),
                dataType: 'json',
                type: 'get',
                contentType: 'application/json',
                success: function(res) {
                    if (res.code == 200) {
                        // 登录成功
                        layui.use('layer', function() {
                            var layer = layui.layer;
                            layer.msg('登录成功，正在跳转...', {icon: 1, time: 1500});
                        });
                        
                        // 保存用户信息到Cookie
                        Cookies.set("UserName", names, { expires: 7 });
                        Cookies.set("UserPwd", pwds, { expires: 7 });
                        
                        // 延迟跳转到菜单页
                        setTimeout(function() {
                            location.href = '/Process/Menu';
                        }, 1500);
                    } else {
                        // 登录失败
                        layui.use('layer', function() {
                            var layer = layui.layer;
                            layer.msg(res.message || '登录失败，请检查用户名和密码', {icon: 2, time: 3000});
                        });
                    }
                },
                error: function() {
                    layui.use('layer', function() {
                        var layer = layui.layer;
                        layer.msg('网络错误，请重试', {icon: 2, time: 3000});
                    });
                },
                complete: function() {
                    // 恢复按钮状态
                    $btn.html(originalText).prop('disabled', false);
                }
            });
        }

        function rejus() {
            // 注册功能暂时跳转到菜单页
            location.href = '/Process/Menu';
        }
    </script>
</body>
</html>
